package com.ttg.web.redis;

import org.apache.commons.lang3.StringUtils;
import org.redisson.Redisson;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ZQ
 * @version 1.0
 * @Description
 * @date 2021/3/15 20:37
 */
@Configuration
public class RedissonAutoConfig   {

    @Resource
    private RedisProperties redisProperties;
    @Autowired
    private  LettuceConnectionFactory jedisConnectionFactory;
    /**
     *
     * @return
     */
    @Bean
    public Redisson redisson() {
        Config config = new Config();
        if(StringUtils.isBlank(redisProperties.getModel())||RedisConstants.STAND_ALONE.equals(redisProperties.getModel())){
            config.useSingleServer()
                    .setAddress("redis://"+redisProperties.getAddress())
                    .setPingConnectionInterval(30000)
                    .setRetryInterval(5000)
                    .setTimeout(30000)
                    .setConnectTimeout(30000).setConnectionMinimumIdleSize(10);
            if(StringUtils.isNotBlank(redisProperties.getPassword())){
                config.useSingleServer().setPassword(redisProperties.getPassword());
            }
        }else if(RedisConstants.SENTRY.equals(redisProperties.getModel())){
            String address = redisProperties.getAddress();
            String split[]= address.split(",");
            List<String> clusterNodes = new ArrayList<>();
            for (int i = 0; i <split.length; i++) {
                clusterNodes.add("redis://" + split[i]);
            }
            config.useSentinelServers()
                    .addSentinelAddress(clusterNodes.toArray(new String[clusterNodes.size()]))
                    .setPingConnectionInterval(30000)
                    .setRetryInterval(5000)
                    .setTimeout(30000)
                    .setConnectTimeout(30000)
                    .setMasterName(redisProperties.getMasterName());
            if(StringUtils.isNotBlank(redisProperties.getPassword())){
                config.useSentinelServers().setPassword(redisProperties.getPassword());//设置密码
            }
        }else if (RedisConstants.MASTER_SLAVE.equals(redisProperties.getModel())){
            String address = redisProperties.getAddress();
            String split[]= address.split(",");
            List<String> clusterNodes = new ArrayList<>();
            for (int i = 0; i <split.length; i++) {
                clusterNodes.add("redis://" + split[i]);
            }
            config.useClusterServers()
                    .addNodeAddress(clusterNodes.toArray(new String[clusterNodes.size()]))
                    .setPingConnectionInterval(30000)
                    .setRetryInterval(5000)
                    .setTimeout(30000)
                    .setConnectTimeout(30000);
            if(StringUtils.isNotBlank(redisProperties.getPassword())){
                config.useClusterServers().setPassword(redisProperties.getPassword());//设置密码
            }
        }
        return (Redisson) Redisson.create(config);
    }
    @Bean
    RedisMessageListenerContainer container() {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(jedisConnectionFactory);
        return container;
    }


}



